<?php

if(empty($_POST['filename']) || empty($_POST['content'])){
	exit;
}

$filename = preg_replace('/[^a-z0-9\-\_\.]/i','',$_POST['filename']);
//$randomPostfix = rand(1001, 9999) + 'v1';

header("Cache-Control: ");
header("Content-type: text/plain");
//header('Content-Disposition: attachment; filename="'.$filename.$randomPostfix.'.txt'.'"');
//echo $_POST['content'];



//CREATE directory
$dir = 'levels';
if ( !file_exists($dir) ) {
	mkdir ($dir, 0777);
}
/*file_put_contents ($dir.'/'.$filename.$randomPostfix.'.txt', $_POST['content']);*/
file_put_contents ($dir.'/'.$filename.'.txt', $_POST['content']);


/*add new level to the list of levels*/
$levelList = fopen("levels.txt", "a+");
//fwrite($levelList, "\n".$filename.$randomPostfix.'.txt');
fwrite($levelList, "\n".$filename.'.txt');
fclose($levelList);
?>